﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Globalization;
using System.Web.Compilation;

namespace TenderSampleApp.Code
{
    public static class LocalizationExtentions
    {
        public static string Resource(this HtmlHelper htmlHelper, string expression, params object[] args)
        {

            string path = ((RazorView)htmlHelper.ViewContext.View).ViewPath;
            var fields =
                (ResourceExpressionFields)
                (new ResourceExpressionBuilder()).ParseExpression(expression, typeof(string), new ExpressionBuilderContext(path));
            return (!string.IsNullOrWhiteSpace(fields.ClassKey))
                       ? string.Format((string)htmlHelper.ViewContext.HttpContext.GetGlobalResourceObject(
                           fields.ClassKey,
                           fields.ResourceKey,
                           CultureInfo.CurrentUICulture), args)
                       : string.Format((string)htmlHelper.ViewContext.HttpContext.GetLocalResourceObject(
                           path,
                           fields.ResourceKey,
                           CultureInfo.CurrentUICulture), args);
        }
    }

}